﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionAutoEcoleModel;
using log4net;
using GestionAutoEcole.DAO.Util;
using System.Data.Objects;

namespace GestionAutoEcole.DAO.Impl
{
    public class LeconDaoImpl : BaseRepository<lecon>
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(LeconDaoImpl));

        private static volatile LeconDaoImpl instance;
        private static object syncRoot = new Object();

        private LeconDaoImpl() { }

        public static LeconDaoImpl getInstance()
        {
            if (instance == null)
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new LeconDaoImpl();
                }
            }

            return instance;
        }
        //création d'un groupe
        public override void Create(lecon el)
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin Create");
                }
                using (var context = new autoecolebase())
                {
                    // il faut plutot utiliser ça : context.VUGroupe.AddObject(grp);
                    context.lecon.AddObject(el);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error("Create : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End Create");
                }
            }
        }

        //modification
        public override void Update(lecon entity)
        {

        }


        //Supprimer tous les groupe
        public override void DeleteAll()
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin DeleteAll");
                }
                using (var context = new autoecolebase())
                {
                    var leconList = context.lecon;
                    foreach (var el in leconList)
                    {
                        context.DeleteObject(el);
                    }
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error("DeleteAll : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End DeleteAll");
                }
            }
        }

        //Supprimer un groupe
        public override void Delete(lecon el)
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin Delete");
                }
                using (var context = new autoecolebase())
                {
                    var pers = context.lecon.Where(u => u.LECON_ID == el.LECON_ID).First();
                    context.DeleteObject(pers);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error("Delete : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End Delete");
                }
            }
        }

        //Récupérer tous les groupes
        public override List<lecon> GetList()
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin GetList");
                }
                using (var context = new autoecolebase())
                {
                    return context.lecon.ToList();
                }
            }
            catch (Exception ex)
            {
                log.Error("GetList : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End GetList");
                }
            }
        }

        //Récupérer un groupe avec son Id
        public override lecon FindById(int id)
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin FindById");
                }
                using (var context = new autoecolebase())
                {
                    var grp = context.lecon.Where(u => u.LECON_ID == id);
                    return grp.First();
                }
            }
            catch (Exception ex)
            {
                log.Error("FindById : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End FindById");
                }
            }
        }

        //Récupérer une liste d'groupes avec un critère de recherche
        public override List<lecon> FindByCriteria(Object criteria)
        {
            try
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Begin FindByCriteria");
                }
                using (var context = new autoecolebase())
                {

                    StringBuilder queryString =
                        new StringBuilder(@"SELECT VALUE grp FROM autoecolebase.lecon as grp");
                    SelectBuilder sb = new SelectBuilder();

                    EntitySearch entity = (EntitySearch)criteria;
                    lecon grp = (lecon)entity.Entity;

                    if (grp.LECON_ID != 0)
                    {
                        sb.AndSearchLike("cast(grp.LECON_ID as System.String)", grp.LECON_ID.ToString());
                    }

                    if (grp.COURS_ID != 0)
                    {
                        sb.AndSearchLike("cast(grp.COURS_ID as System.String)", grp.COURS_ID.ToString());
                    }

                    if (grp.ELEVE_ID != 0)
                    {
                        sb.AndSearchLike("cast(grp.ELEVE_ID as System.String)", grp.ELEVE_ID.ToString());
                    }

                    if (grp.EMPLOYEE_ID != 0)
                    {
                        sb.AndSearchLike("cast(grp.EMPLOYEE_ID as System.String)", grp.EMPLOYEE_ID.ToString());
                    }

                    if (grp.VEHICULE_ID != 0)
                    {
                        sb.AndSearchLike("cast(grp.VEHICULE_ID as System.String)", grp.VEHICULE_ID.ToString());
                    }

                    // Critère de date lecon (Date1Debut, Date1Fin sont les dates de recherche sais par l'utilisateur)
                    if (entity.Date1Debut != null && !entity.Date1Debut.Equals(""))
                    {
                        if (entity.Date1Fin != null && !entity.Date1Fin.Equals(""))
                        {
                            sb.AndSearchBetween("cast(grp.DATE_LECON as System.String)", entity.Date1Debut, entity.Date1Fin);
                        }
                        else
                        {
                            sb.AndSearchAfter("cast(grp.DATE_LECON as System.String)", entity.Date1Debut);
                        }
                    }
                    else if (entity.Date1Fin != null && !entity.Date1Fin.Equals(""))
                    {
                        sb.AndSearchBefore("cast(grp.DATE_LECON as System.String)", entity.Date1Fin);
                    }


                    queryString.Append(sb.getQueryString());
                    ObjectQuery<lecon> query = new ObjectQuery<lecon>(queryString.ToString(), context);
                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                log.Error("FindByCriteria : ", ex);
                throw;
            }
            finally
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("End FindByCriteria");
                }
            }

        }


    }
}
